package controller;

import net.sf.json.JSONObject;
import util.JsonUtil;
import entity.JsonRequest;
import exception.MultipleValidationErrors;

public class JsonRequestCtrl {

	private DumpCtrl dumpCtrl = new DumpCtrl();

	public JSONObject processDumpUserRequest(String request) {
		JsonRequest r = null;

		// construct a new JSON Request object
		try {
			r = new JsonRequest(request, "userid");
		} catch (MultipleValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		// relay to Dump Controller
		return dumpCtrl.dumpUser(r.getRequest().getString("userid"));
	}

	public JSONObject processDumpBidRequest(String request) {
		JSONObject r = null;

		// construct a new JSON Request object
		try {
			r = new JsonRequest(request, "course", "section").getRequest();
		} catch (MultipleValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		// relay to Dump Controller
		return dumpCtrl.dumpBid(r.getString("course"), r.getString("section"));
	}

	public JSONObject processDumpSectionRequest(String request) {
		JSONObject r = null;

		// construct a new JSON Request object
		try {
			r = new JsonRequest(request, "course", "section").getRequest();
		} catch (MultipleValidationErrors e) {
			return JsonUtil.newJsonErrorResponse(e.toJsonArray());
		}

		// relay to Dump Controller
		return dumpCtrl.dumpSection(r.getString("course"),
				r.getString("section"));
	}

}
